/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.ApplicationConfig;
import com.ruipengkj.bean.TGift;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.MemberGiftModel;
import com.ruipengkj.model.OrderTableCellEditor;
import com.ruipengkj.model.OrderTableCellRenderer;
import com.ruipengkj.service.TGiftService;
import com.ruipengkj.view.base.BasePagePanel;
/**
 * 说明：
 * 领取礼包UI
 * @Author：黄盛强
 * @Date：2012-03-20
 */
public class MemberGiftPanel extends BasePagePanel<TGift> {
	
	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent numTF = null; //会员编号
	private JButton checkButton = null; //查看订单
	private JLabelJTextFieldComponent telTF = null; //手机号
	
	private JLabelJTextFieldComponent cardNumTF = null; //卡号
	private JLabelJTextFieldComponent nameTF = null; //会员名
	
	private JLabelJTextFieldComponent cumtrapzTF = null; //积分
	private JLabelJTextFieldComponent expenseNumTF = null; //消费次数
	private JLabelJTextFieldComponent expenseMoneyTF = null; //消费金额
	
	private JTable table = null;
	private JScrollPane tableJsp = null;
	
	private MemberGiftModel memberGiftModel;
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TGift> pageView;//翻页主键
	private TGiftService giftService;
	
	public MemberGiftPanel() {
		this.setLayout(new BorderLayout());
		this.initViews();
		this.initTableButton();
		this.initListener();
	}
	
	
	private void initViews() {
		memberGiftModel = new MemberGiftModel();
		table = uiFactory.createRPJTable(memberGiftModel, null, this, 30, JLabel.CENTER);;
		tableJsp = new JScrollPane(table);
		
		
		pageView=new PageView<TGift>();
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		//礼品列表：
		this.add(topPanel(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(pagingComponent, BorderLayout.SOUTH);
		TableRowSorter<MemberGiftModel> tableRowSorter = new TableRowSorter<MemberGiftModel>(memberGiftModel);  
		table.setRowSorter(tableRowSorter);
	}
	
	private JPanel topPanel(){
		FlowLayout flowLayout=new FlowLayout(FlowLayout.LEFT);
		JPanel top_jp=new JPanel();
		BoxLayout boxlayout=new BoxLayout(top_jp,BoxLayout.Y_AXIS);
		top_jp.setLayout(boxlayout);
		
		JPanel jp1=new JPanel(flowLayout);
		numTF = new JLabelJTextFieldComponent("会员编号：",20);
		checkButton = new JButton("查看订单");
		telTF = new JLabelJTextFieldComponent("手 机 号：",20);
		jp1.add(numTF);
		jp1.add(checkButton);
		jp1.add(telTF);
		
		JPanel jp2=new JPanel(flowLayout);
		cardNumTF = new JLabelJTextFieldComponent("卡    号：",20);
		nameTF = new JLabelJTextFieldComponent("会 员 名：",20);
		jp2.add(cardNumTF);
		jp2.add(nameTF);
		
		JPanel jp3=new JPanel(flowLayout);
		cumtrapzTF = new JLabelJTextFieldComponent("积    分：",20);
		expenseNumTF = new JLabelJTextFieldComponent("消费次数：",20);
		expenseMoneyTF = new JLabelJTextFieldComponent("消费金额：",20);
		jp3.add(cumtrapzTF);
		jp3.add(expenseNumTF);
		jp3.add(expenseMoneyTF);
		Util.setSize(jp1, ApplicationConfig.APP_WIDTH-30, 40);
		Util.setSize(jp2, ApplicationConfig.APP_WIDTH-30, 40);
		Util.setSize(jp3, ApplicationConfig.APP_WIDTH-30, 40);
		top_jp.add(jp1);
		top_jp.add(jp2);
		top_jp.add(jp3);
		return top_jp;
	}
	
	private void initTableButton() {
		//查看按钮
		OrderTableCellEditor orderTableCellEditor=new OrderTableCellEditor();
		OrderTableCellRenderer orderTableCellRenderer=new OrderTableCellRenderer(orderTableCellEditor);
		TableColumn tableColumn = table.getColumnModel().getColumn(table.getColumnCount() - 1);
		tableColumn.setCellRenderer(orderTableCellRenderer);
		tableColumn.setCellEditor(new OrderTableCellEditor());
		
		//兑换按钮
		OrderTableCellEditor orderTableCellEditor2=new OrderTableCellEditor();
		OrderTableCellRenderer orderTableCellRenderer2=new OrderTableCellRenderer(orderTableCellEditor2);
		TableColumn tableColumn2 = table.getColumnModel().getColumn(table.getColumnCount() - 2);
		tableColumn2.setCellRenderer(orderTableCellRenderer2);
		tableColumn2.setCellEditor(new OrderTableCellEditor());
	}
	private void initListener(){
		checkButton.addActionListener(this);
		numTF.getText().addKeyListener(this);
	}
	
	public void run() {
		this.termQuery(1);
	}
	//条件查询
	@Override
	protected void termQuery(int currentPage){
		TGift tgift=new TGift();
		tableJsp.getViewport().remove(table);
		tableJsp.getViewport().add(waitComponent);
		try {
			pageView.setCurrentPage(currentPage);
			PageModel<TGift> pageModel=giftService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tgift);
			pageView.setPageModel(pageModel);
			pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
			if(pageModel!=null){
				tableJsp.getViewport().remove(waitComponent);
				tableJsp.getViewport().add(table);
				memberGiftModel.setRowData(pageModel.getResultList());
				table.updateUI(); 
				pagingComponent.show();
			}else{
				//没有订单数据
				tableJsp.getViewport().remove(waitComponent);
				tableJsp.getViewport().add(table);
				memberGiftModel.clear();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	protected PageView<TGift> getPageView() {
		return pageView;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==checkButton){
			//查询
		}
	}
	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getSource()==numTF.getText()){
			//查询会员
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				checkButton.doClick();
			}
		}
	}
	
	@Override
	public void clear() {
		
	}
}
